import * as TYPE from '../action-types';
import { getSliders, getLessons } from "@/api/home";
import {StoreGetState, StoreDispatch} from '../index'
export default {
    setCurrentCategory(currentCategory: string) {
        return { type: TYPE.SET_CURRENT_CATRGRY, payload: currentCategory }
    },
    getLessons() {
        return (dispatch: StoreDispatch, getState: StoreGetState) => {
            (async function () {
                let {
                    currentCategory,
                    lessons: { hasMore, offset, limit, loading },
                } = getState().home;
                if (hasMore && !loading) {
                    dispatch({ type: TYPE.SET_LESSONS_LOADING, payload: true });
                    let result = await getLessons(currentCategory, offset, limit);
                    dispatch({ type: TYPE.SET_LESSONS, payload: result.data });
                }
            })();
        };
    },
    refreshLessons() {
        return (dispatch: StoreDispatch, getState: StoreGetState) => {
            (async function () {
                let {
                    currentCategory,
                    lessons: { limit, loading },
                } = getState().home;
                if (!loading) {
                    dispatch({ type: TYPE.SET_LESSONS_LOADING, payload: true });
                    let result = await getLessons(currentCategory, 0, limit);
                    dispatch({ type: TYPE.REFRESH_LESSONS, payload: result.data });
                }
            })();
        };
    },
}